package com.ht.WeNas.utils;


import com.ht.WeNas.data.dto.UserDto;
import com.ht.WeNas.utils.exceptions.UserNotLoginException;

public class ThreadLocalUtil {
    static final ThreadLocal<Long> userIdTL = new ThreadLocal<>();
    static final ThreadLocal<UserDto> userTL = new ThreadLocal<>();

    public static void setUserId(long uid) {
        userIdTL.set(uid);
    }

    public static Long getUserId() throws UserNotLoginException {
        Long l = userIdTL.get();
        if (l == null) {
            throw new UserNotLoginException();
        }
        return l;
    }

    /**
     * 清理所有ThreadLocal数据，防止线程池窜数据
     */
    public static void removeAll() {
        userTL.remove();
    }

    public static void setUserDto(UserDto UserDto) {
        userTL.set(UserDto);
    }

    public static UserDto getUserDto() throws UserNotLoginException {
        UserDto dto = userTL.get();
        if (dto == null) {
            throw new UserNotLoginException();
        }
        return dto;
    }
}